﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using ServiceStack.WebHost.Endpoints;
using MMM.Services;
using MMM.Core.Data;
using MMM.Core;
using MMM.Services.Type;
using ServiceStack.ServiceHost;
using ServiceStack.CacheAccess;
using ServiceStack.CacheAccess.Providers;
using ServiceStack.ServiceInterface;
using MMM.Services.Operations;
using ServiceStack.Common.Web;

namespace BLBTSystem
{
    public class Global : System.Web.HttpApplication
    {
        public class HelloAppHost : AppHostBase
        {
            //Tell Service Stack the name of your application and where to find your web services
            public HelloAppHost()
                : base("Hello Web Services", typeof(FunctionService).Assembly) { }

            public override void Configure(Funq.Container container)
            {
                SetConfig(new EndpointHostConfig()
                {
                    DefaultContentType = ContentType.Json,
                    GlobalResponseHeaders =
                    {
                        { "Access-Control-Allow-Origin", "*" },
                        { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
                    },

                });

                // register storage for user sessions 
                container.Register<ICacheClient>(new MemoryCacheClient());

                // Register AuthFeature with custom user session and custom auth provider
                Plugins.Add(new AuthFeature(
                    () => new MyUserSession(),
                    new[] { new MyCredentialsAuthProvider() }
                ));

                ApplicationContext.Start(DbCnnFactoryManager.CreateCnnFactory("sqlserver", DbProvider.MsSqlProvider));
                TableManager.CreateTableWithSameNameSpace<Function>(false);

            }

            private static Feature GetDisabledFeatures()
            {
                //var disabled = ConfigurationManager.AppSettings.Get("AbledFeatures");

                //Feature d;
                //if (!string.IsNullOrWhiteSpace(disabled)
                //&& Enum.TryParse(disabled, true, out d))
                //{
                //    return d;
                //}
                return Feature.Json;
            }
        }
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码

            //Initialize your application
            var appHost = new HelloAppHost();
            appHost.Init();

        }


    }
}
